package com.nikhu.fx.web;

import java.util.List;

import com.nikhu.fx.bom.ExchangeRate;
import com.nikhu.fx.services.CurrencyConverter;

public class CurrencyConverterBean {
	private String fromCurrency = "USD";
	private String toCurrency = "INR";
	private double amount = 100;
	private double conversionAmount;
	private CurrencyConverter currencyConverter;

	public List<ExchangeRate> getExchangeRates() {
		return getCurrencyConverter().currencyRates();
	}

	public double exchangeRate() {
		List<ExchangeRate> currencyRates = getCurrencyConverter()
				.currencyRates();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		double toExchangeRate = 0;
		double fromExchangeRate = 0;
		for (ExchangeRate exchangeRate : currencyRates) {
			if (exchangeRate.getSymbol().equalsIgnoreCase(fromCurrency)) {
				fromExchangeRate = exchangeRate.getRate();
			}
			if (exchangeRate.getSymbol().equalsIgnoreCase(toCurrency)) {
				toExchangeRate = exchangeRate.getRate();
			}
		}
		conversionAmount = amount * (fromExchangeRate / toExchangeRate);
		System.out.println("Conversion Amount:" + conversionAmount);
		return conversionAmount;
	}

	public CurrencyConverter getCurrencyConverter() {
		return currencyConverter;
	}

	public void setCurrencyConverter(CurrencyConverter currencyConverter) {
		this.currencyConverter = currencyConverter;
	}

	public List<ExchangeRate> getCurrencyRates() {
		return currencyConverter.currencyRates();
	}

	public void setCurrencyRates(List<ExchangeRate> currencyRates) {

	}

	public String getToCurrency() {
		return toCurrency;
	}

	public void setToCurrency(String toCurrency) {
		this.toCurrency = toCurrency;
	}

	public String getFromCurrency() {
		return fromCurrency;
	}

	public void setFromCurrency(String fromCurrency) {
		this.fromCurrency = fromCurrency;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public double getConversionAmount() {
		return conversionAmount;
	}

	public void setConversionAmount(double conversionAmount) {
		this.conversionAmount = conversionAmount;
	}
}
